//
//  BIDFastReplyView.m
//  shangwuting
//
//  Created by mal on 14/12/10.
//  Copyright (c) 2014年 mal. All rights reserved.
//

#import "BIDFastReplyView.h"
#import "BIDFastReplyCell.h"
#import "BIDGlobalMethods.h"

@interface BIDFastReplyView()<UITableViewDataSource, UITableViewDelegate>
{
    UITableView *_myTableView;
    NSArray *_replyArr;
    NSMutableArray *_heightArr;
    BOOL _bRegister;
}

@end

@implementation BIDFastReplyView
@synthesize delegate;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self prepareForData];
    }
    return self;
}

- (void)prepareForData
{
    self.autoresizesSubviews = YES;
    
    _replyArr = @[@"全天在办公室处理相关工作", @"全天在机关参加会议", @"上午参加商务工作汇报会，下午在办公室工作", @"请假"];
    _heightArr = [[NSMutableArray alloc] init];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    [self setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
    for(NSString *str in _replyArr)
    {
        int height = [BIDGlobalMethods getHeightWithString:str font:[UIFont systemFontOfSize:17.0f] constraintSize:CGSizeMake(screenSize.width-8*2, MAXFLOAT)];
        [_heightArr addObject:[NSNumber numberWithInt:height]];
    }
    _myTableView = [[UITableView alloc] init];
    _myTableView.frame = self.bounds;
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self addSubview:_myTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _replyArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDFastReplyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDFastReplyCell *fastReplyCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    fastReplyCell.contentLabel.text = _replyArr[row];
    CGRect frame = fastReplyCell.contentLabel.frame;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    frame.size.width = screenSize.width - 8*2;
    NSNumber *value = _heightArr[row];
    frame.size.height = [value intValue];
    fastReplyCell.contentLabel.frame = frame;
    cell = fastReplyCell;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSNumber *value = _heightArr[row];
    return 6 + [value intValue];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSString *str = _replyArr[row];
    [delegate toReplyWithContent:str];
}

@end
